package com.farm.facade;

import com.farm.equipment.Equipment;
import com.farm.equipment.factory.EquipmentFactory;
import com.farm.equipment.factory.SeedingMachineFactory;
import com.farm.equipment.states.WorkingState;
import com.farm.monitor.*;
import com.farm.path.PathFinder;
import com.farm.path.PathStrategy;
import com.farm.path.AStarPathStrategy;

public class FarmSystemFacade {
    private EquipmentFactory equipmentFactory;
    private EnvironmentSensor soilMoistureSensor;
    private PathStrategy pathStrategy;
    private PathFinder pathFinder;
    private Equipment equipment;

    public FarmSystemFacade() {
        this.equipmentFactory = new SeedingMachineFactory();
        this.soilMoistureSensor = new EnvironmentSensor();
        this.pathStrategy = new AStarPathStrategy();
    }

    public FarmSystemFacade(Equipment equipment, PathFinder pathFinder ) {
        this.soilMoistureSensor = new EnvironmentSensor();
        this.equipment=equipment;
        this.pathFinder=pathFinder;
    }

    public void startFarmOperation() {
        System.out.println("=== 启动农场作业系统 ===");
//        // 1. 创建设备
//        Equipment equipment = equipmentFactory.createEquipment("SM001", "播种机1号");
        // 创建观察者
        SensorObserver tempMonitor = new TemperatureMonitor();
        SensorObserver humidityMonitor = new HumidityMonitor();
        SensorObserver pressureMonitor = new PressureMonitor();
        // 注册观察者
        soilMoistureSensor.registerObserver(tempMonitor);
        soilMoistureSensor.registerObserver(humidityMonitor);
        soilMoistureSensor.registerObserver(pressureMonitor);

        // 2. 测量土壤数据
        System.out.println("\n开始测量土壤数据...");
        // 模拟传感器数据变化
        System.out.println("=== 第一次数据更新 ===");
        soilMoistureSensor.setMeasurements(25.5, 60.0, 1013.2);
        System.out.println("\n=== 第二次数据更新 ===");
        soilMoistureSensor.setMeasurements(32.0, 75.0, 1005.5);
        // 移除一个观察者
        soilMoistureSensor.removeObserver(humidityMonitor);
        System.out.println("\n=== 第三次数据更新 (已移除湿度监控器) ===");
        soilMoistureSensor.setMeasurements(22.0, 90.0, 1020.0);

        
        // 3. 规划路径
        System.out.println("\n开始路径规划...");
        if(pathFinder!=null){
            System.out.println(pathFinder.findPath("A","B"));
        }else {
            System.out.println(pathStrategy.calculatePath("",""));
        }

        
        // 4. 启动设备
        System.out.println("\n启动设备...");
        equipment.setState(new WorkingState());
        System.out.println(equipment);
        equipment.work();
    }

    public void stopFarmOperation() {
        System.out.println("\n=== 停止农场作业系统 ===");
        System.out.println("1. 停止所有设备");
        System.out.println("2. 保存传感器数据");
        System.out.println("3. 生成作业报告");
    }
} 